package com.nation.controller;

import com.nation.domain.*;
import com.nation.service.DevelopmentService;
import com.nation.service.NationInfoService;
import com.nation.utils.ReadAndStoreFile;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dqd on 2017/11/3.
 */
@Controller
@RequestMapping(value = "/Development")
public class DevelopmentController {
    @Autowired
    DevelopmentService DevelopmentService;

    @Autowired
    NationInfoService NationInfoService;
    /**
     * 经济发展
     * @param map
     * @return
     */
    @RequestMapping(value = "/development")
    public String development(ModelMap map){
        List<VEconomicAndDevelopment> elist=DevelopmentService.findAll();
        map.put("elist",elist);
        return "Development/development";
    }

    /**
     * 修改信息页面
     * @param country_code  国家代码
     * @param map
     * @return
     */
    @RequestMapping(value = "/development_update")
    public String development_update(String country_code,ModelMap map){
        VEconomicAndDevelopment development=DevelopmentService.findByCountryCode(country_code);
        map.put("development",development);
        return "Development/development-update";
    }

    /**
     * 修改信息
     * @param VEconomicAndDevelopment 用实体类存储相应信息
     * @return
     */
    @RequestMapping(value = "/development_update.do")
    @ResponseBody
    public String development_pupdate_do(VEconomicAndDevelopment VEconomicAndDevelopment){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        VEconomicAndDevelopment.setRecoder_time(sdf.format(System.currentTimeMillis()));
        Boolean judge=DevelopmentService.update(VEconomicAndDevelopment);
        if(judge)
            return "修改成功";
        else
            return "修改失败";
    }
    @RequestMapping(value = "/development_add.do")
    @ResponseBody
    public String development_add(HttpServletRequest request, @RequestParam(value = "picture",required = false)MultipartFile[] pictures,String statistics, String conferences, String country_code, String currency, String economy, String industry, String agriculture,
                                  String service_industry, String tourism, String hi_tech_industry,
                                  String reference, String traffic_transportation, String external_trade, String finance_monetary, String inverstment_abroad,
                                  String foreign_aid, String international_aid,
                                  String top_company, String economic_organization){

        VEconomicAndDevelopment VEconomicAndDevelopment=new VEconomicAndDevelopment(statistics,conferences,country_code,currency,economy,industry,agriculture
                ,service_industry,tourism,hi_tech_industry
                ,reference,traffic_transportation,external_trade,
                finance_monetary,inverstment_abroad,foreign_aid,international_aid
                ,top_company,economic_organization);
        User user= (User) SecurityUtils.getSubject().getPrincipal();
        VEconomicAndDevelopment.setRecoder_id(user.getYHZH());
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        VEconomicAndDevelopment.setRecoder_time(sdf.format(System.currentTimeMillis()));
        Boolean judge=null;
        try{
            if (pictures!=null&&pictures.length>0) {//有文件上传
                String path=request.getSession().getServletContext().getResource("/").getPath();
                path += ReadAndStoreFile.ECONONNY;
                System.out.println("path--"+path);

                String picture_path="";
//          将MultipartFile格式转换成File
                for (int i=0;i<pictures.length;i++){
                    String temp=ReadAndStoreFile.save(pictures[i],path);
                    temp=temp.substring(temp.indexOf("nation/"+ReadAndStoreFile.ECONONNY));
                    if (i == (pictures.length-1) || pictures.length == 1){
                        picture_path=temp+picture_path;
                    }else{
                        picture_path+=temp+",";
                    }
                }
                VEconomicAndDevelopment.setPicture(picture_path);
            }

            judge=DevelopmentService.insert(VEconomicAndDevelopment);
        }catch (Exception e){
            e.printStackTrace();
            return "添加失败";
        }

        if(judge)
            return "添加成功";
        else
            return "添加失败";
    }
    @RequestMapping(value = "/development_add")
    public String development_add(){
        return "Development/development-add";
    }

    @RequestMapping(value = "/InfoSearch")
    public String InfoSearch(HttpServletRequest request,ModelMap mmap){
        String recoder_id=request.getParameter("recoder_id");
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("recoder_id",recoder_id);
        List<VEconomicAndDevelopment> elist=DevelopmentService.findByCondition(map);
        mmap.put("elist",elist);
        return "Development/development";
    }
    @RequestMapping(value = "/development_delete")
    @ResponseBody
    public String development_delete(String country_code){
        Boolean judge=null;
        try{
            judge=DevelopmentService.delete(country_code);
        }catch (Exception e){
            e.printStackTrace();
            return "删除失败";
        }
        if(judge)
            return "删除成功";
        else
            return "删除失败";
    }
}
